/**
 * 
 */
package com.nari.interfaces.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;
import com.nari.interfaces.model.IMissionFailureLog;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;

/**
 * 
 * @author Xiaopeng
 * @date 2013-12-8
 */
public class MissionFailureLogDaoImpl extends GenericDaoImpl<IMissionFailureLog, String> 
		implements IMissionFailureLogDao {

	/**
	 * 分页查询上传失败的任务数据
	 * @param measureMeterNo 上传任务对应的集中器号/热表表号(可选)
	 * @param recordTimeStart 上传任务对应的抄表日期起始日期(可选)
	 * @param recordTimeEnd 上传任务对应的抄表日期结束日期(可选)
	 * @param pageCond 
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-12-9
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<IMissionFailureLog> queryAuditLogPage(String measureMeterNo, 
			String recordTimeStart, String recordTimeEnd, PageCond pageCond) throws Exception {
		Criteria listCriteria = this.getCurrentSession().createCriteria(IMissionFailureLog.class);
		Criteria countCriteria = this.getCurrentSession().createCriteria(IMissionFailureLog.class);
		//集中器号
		if(measureMeterNo!=null && !"".equals(measureMeterNo.trim())) {
			listCriteria.add(Restrictions.ilike("measureMeterNo", measureMeterNo.trim().toLowerCase(), MatchMode.ANYWHERE));
			countCriteria.add(Restrictions.ilike("measureMeterNo", measureMeterNo.trim().toLowerCase(), MatchMode.ANYWHERE));
		}
		//抄表日期
		if((recordTimeStart!=null && !"".equals(recordTimeStart.trim())) 
				&& (recordTimeEnd==null || "".equals(recordTimeEnd.trim()))) {
			listCriteria.add(Restrictions.ge("recordTime", recordTimeStart));
			countCriteria.add(Restrictions.ge("recordTime", recordTimeStart));
		} else if((recordTimeStart==null || "".equals(recordTimeStart.trim())) 
				&& (recordTimeEnd!=null && !"".equals(recordTimeEnd.trim()))) {
			listCriteria.add(Restrictions.le("recordTime", recordTimeEnd));
			countCriteria.add(Restrictions.le("recordTime", recordTimeEnd));
		} else if((recordTimeStart!=null && !"".equals(recordTimeStart.trim())) 
				&& (recordTimeEnd!=null && !"".equals(recordTimeEnd.trim()))) {
			listCriteria.add(Restrictions.between("recordTime", recordTimeStart.trim(), recordTimeEnd.trim()));
			countCriteria.add(Restrictions.between("recordTime", recordTimeStart.trim(), recordTimeEnd.trim()));
		}
		//统计结果集个数
		int total = ((Long)countCriteria.setProjection(Projections.rowCount()).list().get(0)).intValue();
		//结果集排序
		listCriteria.addOrder(Order.desc("auditTime")).addOrder(Order.desc("recordTime"));
		//结果集分页
		listCriteria.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
		//构造返回类型
		pageCond.setTotal(total);
		ResultWithPageCond<IMissionFailureLog> missionLogPage = new ResultWithPageCond<IMissionFailureLog>();
		missionLogPage.setList(listCriteria.list());
		missionLogPage.setPageCond(pageCond);
		return missionLogPage;
	}
	
	/**
	 * 根据审计日志的标志性信息统计审计日志的个数
	 * @param measureMeterNo
	 * @param recordTime
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-6-19
	 */
	public int countAuditLogBySymbol(String measureMeterNo, String recordTime) throws Exception {
		String hsql = "select count(*) from IMissionFailureLog log " +
				"where log.measureMeterNo like '%"+measureMeterNo+"' " +
				"and log.recordTime='"+recordTime+"'";
		return ((Long)this.getCurrentSession().createQuery(hsql).uniqueResult()).intValue();
	}
	
	/**
	 * 根据审计日志的标志性信息查询审计日志信息
	 * @param measureMeterNo
	 * @param recordTime
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-6-19
	 */
	public List<IMissionFailureLog> queryAuditLogBySymbol(String measureMeterNo, 
			String recordTime) throws Exception {
		String hsql = "from IMissionFailureLog log " +
				"where log.measureMeterNo like '%"+measureMeterNo+"' " +
				"and log.recordTime='"+recordTime+"' " +
				"order by log.auditTime desc";
		return this.runQueryHSql(hsql);
	}
}
